<template>
	<form class='loginView' @submit="login">
		<view class="input-view">
			<view class="label-view">
				<text class="label">账号 </text>
			</view>
			<input class="input" type="text" placeholder="请输入用户名" name="num" />
		</view>
		<view class="input-view">
			<view class="label-view">
				<text class="label">密码</text>
			</view>
			<input class="input" type="password" placeholder="请输入密码" name="pwd" />
		</view>
		<view class="button-view">
			<button type="default" class="login" hover-class="hover" formType="submit">登录</button>
			<button type="default" class="register" hover-class="hover" @click="register">免费注册</button>
		</view>
	</form>
</template>

<script>
	export default {
		data() {
			return {};
		},
		methods: {
			login(e) {
				//请求登录接口
				uni.request({
					url: this.$serverUrl + 'login',
					method: 'POST',
					header: {
						'content-type': 'application/json' //自定义请求头信息
					},
					data: {
						'num': e.detail.value.num,
						'pwd': e.detail.value.pwd
					},
					success: (res) => {
						var data = res.data
						uni.showLoading({
							title: data.msg
						});
						setTimeout(function() {
							if (data.code == 200) {
								//存入缓存,并跳转到系统个人中心页面
								//jwt缓存
								uni.setStorageSync('token',data.data.loginUserCode)
								//用户基本信息缓存
								uni.setStorageSync('userInfo',data.data.userInfo)
								//跳转到个人中心页面
								uni.switchTab({
									url: "../center/center"
								})
							}
							uni.hideLoading();
						}, 500);
					}
				});
			},
			register() {
				//跳转到注册页面
				uni.navigateTo({
					url: "../register/register"
				})
			}
		}
	}
</script>

<style>

</style>
